package com.musicxapp.modules.util;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.Response;

public class QQRequest {
    private static OkHttpClient client;
    static {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.addInterceptor(new Interceptor() {
            @NonNull
            @Override
            public Response intercept(@NonNull Chain chain) throws IOException {
                okhttp3.Request request = chain.request().newBuilder()
                        .addHeader("User-Agent",
                                "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36")
                        .addHeader("Content-Type","application/x-www-form-urlencoded")
                        .build();
                return chain.proceed(request);
            }
        });
        client = builder.build();
    }
    public static Response getRequest(String url, String cookie) throws IOException {



        okhttp3.Request.Builder reqBuilder = new okhttp3.Request.Builder();
        if(url.contains("u.y.qq")){
            reqBuilder.addHeader("Referer","https://u.y.qq.com");
        }else if(url.contains("c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo")){
            reqBuilder.addHeader("Referer","https://y.qq.com/n/yqq/playlist");
        } else {
            reqBuilder.addHeader("Referer","https://y.qq.com");
        }
        reqBuilder.addHeader("Cookie",cookie);
//        FormBody.Builder formBodyBuilder = new FormBody.Builder();
//
//        formBodyBuilder.add("data", JSONObject.toJSONString(data));

//        RequestBody body = formBodyBuilder.build();
        okhttp3.Request postRequest = reqBuilder.url(url).get().build();
        Response response = client.newCall(postRequest).execute();
        return response;
    }

    public static Response postRequest(String url, Map data, String cookie) throws IOException {

        okhttp3.Request.Builder reqBuilder = new okhttp3.Request.Builder();
        if(url.contains("u.y.qq")){
            reqBuilder.addHeader("Referer","https://u.y.qq.com");
        }else if(url.contains("c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo")){
            reqBuilder.addHeader("Referer","https://y.qq.com/n/yqq/playlist");
        } else {
            reqBuilder.addHeader("Referer","https://y.qq.com");
        }
        reqBuilder.addHeader("Cookie",cookie);
        FormBody.Builder formBodyBuilder = new FormBody.Builder();
        if (data != null) {
            for (Object key : data.keySet()) {
                formBodyBuilder.add((String) key, (String)data.get(key));
            }
        }
        RequestBody body = formBodyBuilder.build();
        okhttp3.Request postRequest = reqBuilder.url(url).post(body).build();
        Response response = client.newCall(postRequest).execute();
        return response;
    }
}
